<?php

require_once 'autoload.php';

if ($_GET['act'] == 'check') {

    $session = new Zend_Session_Namespace("gynmg.tp");
    $f_post = $session->post;
    $selected_person = fomrat_to_ids($f_post);
   

    if (trim($selected_person) === "")
    {
            $msg = "你没有选择任何一个候选人，请返回重新选择!";
            $msg .= "<a href=index.php>返回首页</a>";
            showMessage($msg);
    }

    $view = new Core_View("post.html");
    $form = new Core_Form_Info($view->getView());
    if ($form->isValid($_POST)) {

        $v = new Core_Validator();
        $pass_ip = $v->validateIpAddress(get_user_ip());
        $pass_rf = $v->validateReferer($_SERVER['HTTP_REFERER']);
        $pass_pc = $v->validatePersonCount($selected_person);
        $pass_ai = $v->validateBrainFuckAnswer($_POST['rq_answer']);

        if ($pass_ip && $pass_pc && $pass_rf && $pass_ai) {
            $post = $_POST;
            $bind = array(
                'p_ids' => $selected_person,
                'from_ip' => get_user_ip(),
                'user_name' => $post['realname'],
                'user_idcard' => $post['idcard'],
                'user_location' => $post['location'],
                'user_telphone' => $post['telphone'],
                'regist_dt' => new Zend_Db_Expr('NOW()')
            );
            $db = Core_Db::factory();
            $db->insert('vote', $bind);
            Zend_Session::namespaceUnset("gynmg.tp");
            header("Location: post.php?act=thanks");
        } else {
            if ($pass_ip == false) {                
                $msg = "对不起，当前你的IP地址限制已到，请明日再投票！";
                $msg .= "<br />";
                $msg .= "<a href=index.php>返回首页</a>";
                showMessage($msg);
            }
            if ($pass_pc == false) {                
                $msg = "对不起，你最多只能对20个人投票！";
                $msg .= "<br />";
                $msg .= "<a href=index.php>返回首页</a>";
                showMessage($msg);
            }
            if ($pass_rf == false) {                
                showMessage("请不要从外部提交表单！");
            }
            if ($pass_ai == false) {                
                $msg = "随机问题答案错误，请回答正确答案！";
                $msg .= "<br />";
                $msg .= "<a href=index.php>返回首页</a>";
                showMessage($msg);
            }
        }
    } else {
        $form->setAction("post.php?act=check");
        $form->setMethod('post');
        $view->form = $form;
        echo $view->fetch();
    }
}

if ($_GET['act'] == 'save') {
    $session = new Zend_Session_Namespace("gynmg.tp");
    $session->post = $_POST;
    $view = new Core_View("post.html");
    $form = new Core_Form_Info($view->getView());
    $form->setAction("post.php?act=check");
    $form->setMethod('post');
    $view->form = $form;
    echo $view->fetch();
}

if ($_GET['act'] == 'thanks') {
    $msg = "投票操作已完成！谢谢你的投票！";
    $msg .= " <br />";
    $msg .= "<a href=index.php>返回首页</a>";
    showMessage($msg);
}

function get_user_ip() {
    if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] != 'unknown') {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != 'unknown') {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function fomrat_to_ids($posted_array) {
    $str_person = "";
    foreach ($posted_array as $key => $value) {
        $id = str_replace('chkbox_', '', $key);
        if ($str_person == "") {
            $str_person .= $id;
        } else {
            $str_person .= "," . $id;
        }
    }
    return $str_person;
}

function showMessage($message)
{
    $view = new Core_View("message.html");
    $view->message = $message;
    echo $view->fetch();
}